package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMysignuplistBinding;
import com.sgb.kjwl.model.entity.enterprise.MyNeedQuoteListEntity;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.MySignUpListAdpater;
import com.sgb.kjwl.view.widget.ClearEditText;
import com.sgb.kjwl.viewmodel.enterprise.MySignUpListViewModel;
import com.sgb.link.view.widget.CustomXTabLayout;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的报价列表
 */
public class MySignUpListActivity extends AppActivity {

    private ActivityMysignuplistBinding mBinding;
    private MySignUpListViewModel mViewModel;
    private MySignUpListAdpater mAdpater;
    private Map<String, Object> mMap = new HashMap<>();

    @Override
    protected void initView() {
        super.initView();

        mViewModel = new ViewModelProvider(this).get(MySignUpListViewModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_mysignuplist);
        mBinding.setLifecycleOwner(this);

        initRecyclerView();
        initTablayout();
        initLister();


    }

    @Override
    protected void init() {
        mMap.put("status", 0);
        mMap.put("demandTitle", "");
        mMap.put("type", UserOperating.getInstance().getUserIdentity());
        switch (UserOperating.getInstance().getUserIdentity()) {
            case 0:
                mMap.put("no", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO, ""));
                break;
            case 1:
            case 2:
                mMap.put("no", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO, ""));
                break;
        }
        mViewModel.getDemandOfferList(mMap, true);

        mViewModel.showLoading.observe(this, type -> {
            switch (type) {
                case 1:
                    DialogHelper.showProgressDialog(this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case 2:
                    DialogHelper.dismissProgressDialog();
                    break;
            }
        });
        mViewModel.toastUntil.observe(this, message -> {
            MToast.showLongToast(this, message);
        });

        mViewModel.listOffer.observe(this, new Observer<List<MyNeedQuoteListEntity>>() {
            @Override
            public void onChanged(List<MyNeedQuoteListEntity> objects) {
                if (mViewModel.pageSize > 1) {
                    mBinding.recyclerViewTrand.loadMoreComplete();
                    mAdpater.addDatas(objects);
                    if (objects.size() == 0) {
                        mBinding.recyclerViewTrand.setNoMore(true);
                    }
                } else {
                    mAdpater.setDatas(objects);
                    if (mAdpater.getDatas().size() == 0) {
                        mBinding.emptyView.setVisibility(View.VISIBLE);
                        mBinding.include.setVisibility(View.VISIBLE);
                    } else {
                        if (objects.size() == 20) {
                            mBinding.recyclerViewTrand.setLoadingMoreEnabled(true);
                        }
                        mBinding.emptyView.setVisibility(View.GONE);
                    }
                    mBinding.recyclerViewTrand.smoothScrollToPosition(0);
                    mBinding.recyclerViewTrand.refreshComplete();
                }
            }
        });

    }


    private void initRecyclerView() {
        mAdpater = new MySignUpListAdpater(this);
        mBinding.recyclerViewTrand.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerViewTrand.setAdapter(mAdpater);
        mAdpater.setOnItemClick(new AdapterOnItemClick<MyNeedQuoteListEntity>() {
            @Override
            public void onItemClick(MyNeedQuoteListEntity o, int position) {
                MySignUpDetailActivity.start(MySignUpListActivity.this, o.getXqid(), o.getBjid());
            }
        });
        mBinding.recyclerViewTrand.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mViewModel.getDemandOfferList(mMap, true);
            }

            @Override
            public void onLoadMore() {
                mViewModel.getDemandOfferList(mMap, false);
            }
        });
        mAdpater.onClickName(new MySignUpListAdpater.OnClickName() {
            @Override
            public void click(int publishType, String userNo, String teamNo, String companyNo) {
                if (!ClickUtils.isDoubleClick()) {

                    switch (publishType) {
                        case 0:
                            WebsiteInfoActivity.start(MySignUpListActivity.this, userNo, WebsiteInfoActivity.WEBSITE_FOR_PERSONAL);
                            break;
                        case 1:
                            WebsiteInfoActivity.start(MySignUpListActivity.this, companyNo, WebsiteInfoActivity.WEBSITE_FOR_COMPANY);
                            break;
                        case 2:
                            WebsiteInfoActivity.start(MySignUpListActivity.this, teamNo, WebsiteInfoActivity.WEBSITE_FOR_TEAM);
                            break;

                    }
                }
            }
        });

    }


    private void initTablayout() {
        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("全部"));
        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("报价中"));
        mBinding.tabLayout.addTab(mBinding.tabLayout.newTab().setText("报价成功"));

    }


    private void initLister() {
        mBinding.ivBack.setOnClickListener(v -> finish());


        mBinding.rightViewSearch.setOnClickListener((view) -> {
            String title = mBinding.cetSearch.getText().toString();
            mMap.put("demandTitle", title);
            mViewModel.getDemandOfferList(mMap, true);
        });
        mBinding.cetSearch.OnClickClear(new ClearEditText.OnClickClear() {
            @Override
            public void clickClear() {
                mMap.put("demandTitle","");
                mViewModel.getDemandOfferList(mMap, true);
            }
        });

        mBinding.tabLayout.addOnTabSelectedListener(new CustomXTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                if (tab.getText().equals("全部")) {
                    mMap.put("status", 0);
                } else if (tab.getText().equals("报价中")) {
                    mMap.put("status", 1);
                } else if (tab.getText().equals("报价成功")) {
                    mMap.put("status", 2);
                }
                mViewModel.getDemandOfferList(mMap, true);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }


    public static void start(Context mContext) {
        Intent mIntent = new Intent();
        mIntent.setClass(mContext, MySignUpListActivity.class);
        mContext.startActivity(mIntent);
    }
}
